package com.shenmazong.serverwebpoem.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.shenmazong.serverwebpoem.pojo.TbPoetry;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.springframework.stereotype.Component;

import java.util.List;
import java.util.Map;

/**
 * @author 田不平
 * @project server-web-poem
 * @date 2020/3/24 11:26 AM
 * http://www.shenmazong.com
 */

@Mapper
@Component(value = "iTbPoetryMapper")
public interface ITbPoetryMapper extends BaseMapper<TbPoetry> {

    // 诗词列表
    @Select("select a.id,a.title,a.dyid,a.dynasty,a.authorid,a.author,a.content,\n" +
            "IFNULL(b.clicks,0) as clicks,IFNULL(b.collections,0) as collections,IFNULL(b.difficult,0) as difficult,IFNULL(b.voice,0) as voice from tb_poetry as a\n" +
            "left join tb_poetry_info as b on a.id=b.poetryid")
    IPage<Map<String, Object>> selectMapPoetryByPage(Page<Map<String,Object>> page);

    // 根据作者的ID进行诗词分页
    @Select("select a.id,a.title,a.dyid,a.dynasty,a.authorid,a.author,a.content,\n" +
            "IFNULL(b.clicks,0) as clicks,IFNULL(b.collections,0) as collections,IFNULL(b.difficult,0) as difficult,IFNULL(b.voice,0) as voice from tb_poetry as a\n" +
            "left join tb_poetry_info as b on a.id=b.poetryid\n" +
            "where a.authorid=#{authorId}")
    IPage<Map<String, Object>> selectMapPoetry4AuthorByPage(Page<Map<String,Object>> page, @Param("authorId") Integer authorId);

    // 根据点记率进行排行
    @Select("select a.id,a.title,a.dyid,a.dynasty,a.authorid,a.author,a.content,\n" +
            "b.clicks,b.collections,b.difficult,b.voice\n" +
            "from tb_poetry_info as b\n" +
            "left join tb_poetry as a on b.poetryid=a.id\n" +
            "order by b.clicks desc")
    IPage<Map<String, Object>> selectMapPoetry4ClickByPage(Page<Map<String,Object>> page);

    @Select("select * from tb_poetry where id not in (select poetryid from tb_poetry_info where es=1)")
    IPage<TbPoetry> selectList4NotInElastic(Page<TbPoetry> page);

    @Select("select * from tb_poetry where id in\n" +
            "(\n" +
            "select poetryid from tb_poetry_info \n" +
            "where difficult>0 and poetryid not in (select poetryid from tb_question)\n" +
            ")")
    IPage<TbPoetry> selectList4NotInQuestion(Page<TbPoetry> page);

    @Select("select * from tb_poetry as a left join tb_poetry_info as b on a.id=b.poetryid where es is NULL limit 1000")
    List<TbPoetry> selectList4EsIsNULL();
}
